package com.example.dllo.my36kr.news;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by dllo on 16/8/15.
 */
public class NewsAdapter extends BaseAdapter {
    Context mContext;
    SimpleDateFormat mformat;
    NewsBean newsBean;


    public void setNewsBean(NewsBean newsBean) {
        this.newsBean = newsBean;
    }

    public NewsAdapter(Context mContext) {
        this.mContext = mContext;
        mformat = new SimpleDateFormat("HH:mm");
    }


    @Override
    public int getCount() {
        return newsBean.getData().getData().size();
    }

    @Override
    public Object getItem(int position) {
        return newsBean.getData().getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewsViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_news_item, null);
            holder = new NewsViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (NewsViewHolder) convertView.getTag();
        }

        holder.nameTv.setText(newsBean.getData().getData().get(position).getUser().getName());
        holder.titleTv.setText(newsBean.getData().getData().get(position).getTitle());
        Picasso.with(mContext).load(newsBean.getData().getData().get(position).getFeatureImg()).into(holder.featureImgIv);
        Long now = System.currentTimeMillis();
        long dTime = now - Long.valueOf(newsBean.getData().getData().get(position).getPublishTime());
        SimpleDateFormat nFormat = new SimpleDateFormat("dd");
        int nNow = Integer.parseInt(nFormat.format(now)) - Integer.parseInt(nFormat.format(Long.valueOf(newsBean.getData().getData().get(position).getPublishTime())));
        if (nNow >= 1) {
            holder.publishTimeTv.setText(nNow + "天之前");
        }else if (dTime > 1000 * 60 * 60){
            //显示正常日期
            String time = mformat.format(Long.valueOf(dTime));
            holder.publishTimeTv.setText(time);

        } else if (dTime > 1000 * 60) {
            //显示多长分钟之前
            String min = String.valueOf((int) dTime / 1000 / 60);
            holder.publishTimeTv.setText(min + "分钟之前");
        } else {
            //显示多少s之前
            String second = String.valueOf((int) dTime / 1000);
            holder.publishTimeTv.setText(second + "s之前");
        }
        int num = Integer.parseInt(newsBean.getData().getData().get(position).getColumnId());
        if (num == 69) {//资本
            holder.columnNameTv.setTextColor(Color.rgb(0, 0, 255));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 23) {//大公司
            holder.columnNameTv.setTextColor(Color.rgb(135, 206, 250));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 72) {//其他
            holder.columnNameTv.setTextColor(Color.rgb(0, 0, 255));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 67) {//早期项目
            holder.columnNameTv.setTextColor(Color.rgb(0, 250, 154));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 68) {//B轮后
            holder.columnNameTv.setTextColor(Color.rgb(0, 191, 255));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 70) {//深度
            holder.columnNameTv.setTextColor(Color.rgb(255, 105, 180));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        } else if (num == 71) {//研究
            holder.columnNameTv.setTextColor(Color.rgb(255, 165, 0));
            holder.columnNameTv.setText(newsBean.getData().getData().get(position).getColumnName());
        }

        return convertView;
    }

    public class NewsViewHolder {
        ImageView featureImgIv;
        TextView titleTv, nameTv, columnNameTv, publishTimeTv;

        public NewsViewHolder(View view) {
            featureImgIv = (ImageView) view.findViewById(R.id.iv_news_featureImg);
            titleTv = (TextView) view.findViewById(R.id.tv_news_title);
            nameTv = (TextView) view.findViewById(R.id.tv_news_name);
            columnNameTv = (TextView) view.findViewById(R.id.tv_news_columnName);
            publishTimeTv = (TextView) view.findViewById(R.id.tv_news_publishTime);
        }
    }
}
